<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/black/pace-theme-flat-top.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous" defer></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"tryhub.top","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.23.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.json","localsearch":{"enable":true,"top_n_per_article":1,"unescape":true,"preload":true,"trigger":"auto"}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="拦截器 Interceptorjava里的拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码，也可以在一个action执行前阻止其执行，同时也提供了一种可以提取action中可重用部分的方式。在AOP（Aspect-Oriented Programming）中拦截器用于在某个方法或字段被访问之前，进行拦截然后在之前或之后加入某些操作。">
<meta property="og:type" content="article">
<meta property="og:title" content="Struts2拦截器实现用户访问控制">
<meta property="og:url" content="https://tryhub.top/2017/06/14/Struts2%E6%8B%A6%E6%88%AA%E5%99%A8%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6/index.html">
<meta property="og:site_name" content="奇姆的博客">
<meta property="og:description" content="拦截器 Interceptorjava里的拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码，也可以在一个action执行前阻止其执行，同时也提供了一种可以提取action中可重用部分的方式。在AOP（Aspect-Oriented Programming）中拦截器用于在某个方法或字段被访问之前，进行拦截然后在之前或之后加入某些操作。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-06-14T03:00:19.000Z">
<meta property="article:modified_time" content="2023-07-07T07:26:55.964Z">
<meta property="article:author" content="Jaron Chim">
<meta property="article:tag" content="idea">
<meta property="article:tag" content="struts2">
<meta property="article:tag" content="拦截器">
<meta property="article:tag" content="Interceptor">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://tryhub.top/2017/06/14/Struts2%E6%8B%A6%E6%88%AA%E5%99%A8%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://tryhub.top/2017/06/14/Struts2%E6%8B%A6%E6%88%AA%E5%99%A8%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6/","path":"2017/06/14/Struts2拦截器实现用户访问控制/","title":"Struts2拦截器实现用户访问控制"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Struts2拦截器实现用户访问控制 | 奇姆的博客</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js" defer></script>
  <script async src="https://hm.baidu.com/hm.js?1ad13b7aff8bfe9206fa7e12d5c4a4f0"></script>







  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script><script src="/js/bookmark.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>





  <script src="/js/third-party/pace.js" defer></script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="奇姆的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">奇姆的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%A6%E6%88%AA%E5%99%A8-Interceptor"><span class="nav-number">1.</span> <span class="nav-text">拦截器 Interceptor</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-number">2.</span> <span class="nav-text">自定义拦截器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E5%AE%9E%E7%8E%B0Interceptor%E6%8E%A5%E5%8F%A3%EF%BC%88%E6%88%96%E8%80%85%E7%BB%A7%E6%89%BF%E8%87%AAAbstractInterceptor%EF%BC%89%E7%9A%84%E7%B1%BB%E3%80%82"><span class="nav-number">2.1.</span> <span class="nav-text">自定义一个实现Interceptor接口（或者继承自AbstractInterceptor）的类。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8struts-xml%E4%B8%AD%E6%B3%A8%E5%86%8C%E4%B8%8A%E4%B8%80%E6%AD%A5%E4%B8%AD%E5%AE%9A%E4%B9%89%E7%9A%84%E6%8B%A6%E6%88%AA%E5%99%A8%E3%80%82"><span class="nav-number">2.2.</span> <span class="nav-text">在struts.xml中注册上一步中定义的拦截器。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8%E7%9A%84Action%E4%B8%AD%E5%BC%95%E7%94%A8%E4%B8%8A%E8%BF%B0%E5%AE%9A%E4%B9%89%E7%9A%84%E6%8B%A6%E6%88%AA%E5%99%A8%EF%BC%8C%E4%B8%BA%E4%BA%86%E6%96%B9%E4%BE%BF%E4%B9%9F%E5%8F%AF%E5%B0%86%E6%8B%A6%E6%88%AA%E5%99%A8%E5%AE%9A%E4%B9%89%E4%B8%BA%E9%BB%98%E8%AE%A4%E7%9A%84%E6%8B%A6%E6%88%AA%E5%99%A8%EF%BC%8C%E8%BF%99%E6%A0%B7%E5%9C%A8%E4%B8%8D%E5%8A%A0%E7%89%B9%E6%AE%8A%E5%A3%B0%E6%98%8E%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E6%89%80%E6%9C%89%E7%9A%84Action%E9%83%BD%E8%A2%AB%E8%BF%99%E4%B8%AA%E6%8B%A6%E6%88%AA%E5%99%A8%E6%8B%A6%E6%88%AA%E3%80%82"><span class="nav-number">2.3.</span> <span class="nav-text">在需要使用的Action中引用上述定义的拦截器，为了方便也可将拦截器定义为默认的拦截器，这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%8E%E8%BF%87%E6%BB%A4%E5%99%A8%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">3.</span> <span class="nav-text">与过滤器的区别</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8B%A6%E6%88%AA%E5%99%A8%E6%98%AF%E5%9F%BA%E4%BA%8Ejava%E5%8F%8D%E5%B0%84%E6%9C%BA%E5%88%B6%E7%9A%84%EF%BC%8C%E8%80%8C%E8%BF%87%E6%BB%A4%E5%99%A8%E6%98%AF%E5%9F%BA%E4%BA%8E%E5%87%BD%E6%95%B0%E5%9B%9E%E8%B0%83%E7%9A%84%E3%80%82"><span class="nav-number">3.1.</span> <span class="nav-text">拦截器是基于java反射机制的，而过滤器是基于函数回调的。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%87%E6%BB%A4%E5%99%A8%E4%BE%9D%E8%B5%96%E4%BA%8Eservlet%E5%AE%B9%E5%99%A8%EF%BC%8C%E8%80%8C%E6%8B%A6%E6%88%AA%E5%99%A8%E4%B8%8D%E4%BE%9D%E8%B5%96%E4%BA%8Eservlet%E5%AE%B9%E5%99%A8%E3%80%82"><span class="nav-number">3.2.</span> <span class="nav-text">过滤器依赖于servlet容器，而拦截器不依赖于servlet容器。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8B%A6%E6%88%AA%E5%99%A8%E5%8F%AA%E5%AF%B9action%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%8C%E8%80%8C%E8%BF%87%E6%BB%A4%E5%99%A8%E5%87%A0%E4%B9%8E%E5%8F%AF%E4%BB%A5%E5%AF%B9%E6%89%80%E6%9C%89%E8%AF%B7%E6%B1%82%E8%B5%B7%E4%BD%9C%E7%94%A8%E3%80%82"><span class="nav-number">3.3.</span> <span class="nav-text">拦截器只对action起作用，而过滤器几乎可以对所有请求起作用。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8B%A6%E6%88%AA%E5%99%A8%E5%8F%AF%E4%BB%A5%E8%AE%BF%E9%97%AEaction%E4%B8%8A%E4%B8%8B%E6%96%87%E3%80%81%E5%80%BC%E6%A0%88%E9%87%8C%E7%9A%84%E5%AF%B9%E8%B1%A1%EF%BC%8C%E8%80%8C%E8%BF%87%E6%BB%A4%E5%99%A8%E4%B8%8D%E8%83%BD%E3%80%82"><span class="nav-number">3.4.</span> <span class="nav-text">拦截器可以访问action上下文、值栈里的对象，而过滤器不能。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8action%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E9%87%8C%EF%BC%8C%E6%8B%A6%E6%88%AA%E5%99%A8%E5%8F%AF%E4%BB%A5%E5%A4%9A%E8%B5%B7%E8%B0%83%E7%94%A8%EF%BC%8C%E8%80%8C%E8%BF%87%E6%BB%A4%E5%99%A8%E5%8F%AA%E8%83%BD%E5%9C%A8%E5%AE%B9%E5%99%A8%E5%88%9D%E5%A7%8B%E5%8C%96%E6%97%B6%E8%B0%83%E7%94%A8%E4%B8%80%E6%AC%A1%E3%80%82"><span class="nav-number">3.5.</span> <span class="nav-text">在action的生命周期里，拦截器可以多起调用，而过滤器只能在容器初始化时调用一次。</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B3%E9%94%AE%E4%BB%A3%E7%A0%81"><span class="nav-number">4.</span> <span class="nav-text">关键代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#struts-xml-%E6%8B%A6%E6%88%AA%E5%99%A8%E9%85%8D%E7%BD%AE"><span class="nav-number">5.</span> <span class="nav-text">struts.xml 拦截器配置</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jaron Chim"
      src="/images/jaron.gif">
  <p class="site-author-name" itemprop="name">Jaron Chim</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">76</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">95</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">144</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/admin-Jaron" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;admin-Jaron" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:tryhub@tryhub.top" title="E-Mail → mailto:tryhub@tryhub.top" rel="noopener me"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="返回顶部">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tryhub.top/2017/06/14/Struts2%E6%8B%A6%E6%88%AA%E5%99%A8%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/jaron.gif">
      <meta itemprop="name" content="Jaron Chim">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇姆的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Struts2拦截器实现用户访问控制 | 奇姆的博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Struts2拦截器实现用户访问控制
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2017-06-14 11:00:19" itemprop="dateCreated datePublished" datetime="2017-06-14T11:00:19+08:00">2017-06-14</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-07-07 15:26:55" itemprop="dateModified" datetime="2023-07-07T15:26:55+08:00">2023-07-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/struts2/" itemprop="url" rel="index"><span itemprop="name">struts2</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/struts2/idea/" itemprop="url" rel="index"><span itemprop="name">idea</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/struts2/idea/Interceptor/" itemprop="url" rel="index"><span itemprop="name">Interceptor</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/struts2/idea/Interceptor/AbstractInterceptor/" itemprop="url" rel="index"><span itemprop="name">AbstractInterceptor</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="拦截器-Interceptor"><a href="#拦截器-Interceptor" class="headerlink" title="拦截器 Interceptor"></a>拦截器 Interceptor</h2><p>java里的拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码，也可以在一个action执行前阻止其执行，同时也提供了一种可以提取action中可重用部分的方式。在AOP（Aspect-Oriented Programming）中拦截器用于在某个方法或字段被访问之前，进行拦截然后在之前或之后加入某些操作。</p>
<span id="more"></span>

<h2 id="自定义拦截器"><a href="#自定义拦截器" class="headerlink" title="自定义拦截器"></a>自定义拦截器</h2><h3 id="自定义一个实现Interceptor接口（或者继承自AbstractInterceptor）的类。"><a href="#自定义一个实现Interceptor接口（或者继承自AbstractInterceptor）的类。" class="headerlink" title="自定义一个实现Interceptor接口（或者继承自AbstractInterceptor）的类。"></a>自定义一个实现Interceptor接口（或者继承自AbstractInterceptor）的类。</h3><h3 id="在struts-xml中注册上一步中定义的拦截器。"><a href="#在struts-xml中注册上一步中定义的拦截器。" class="headerlink" title="在struts.xml中注册上一步中定义的拦截器。"></a>在struts.xml中注册上一步中定义的拦截器。</h3><h3 id="在需要使用的Action中引用上述定义的拦截器，为了方便也可将拦截器定义为默认的拦截器，这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。"><a href="#在需要使用的Action中引用上述定义的拦截器，为了方便也可将拦截器定义为默认的拦截器，这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。" class="headerlink" title="在需要使用的Action中引用上述定义的拦截器，为了方便也可将拦截器定义为默认的拦截器，这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。"></a>在需要使用的Action中引用上述定义的拦截器，为了方便也可将拦截器定义为默认的拦截器，这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。</h3><h2 id="与过滤器的区别"><a href="#与过滤器的区别" class="headerlink" title="与过滤器的区别"></a>与过滤器的区别</h2><p>过滤器可以简单理解为“取你所想取”，忽视掉那些你不想要的东西；拦截器可以简单理解为“拒你所想拒”，关心你想要拒绝掉哪些东西，比如一个BBS论坛上拦截掉敏感词汇。</p>
<h3 id="拦截器是基于java反射机制的，而过滤器是基于函数回调的。"><a href="#拦截器是基于java反射机制的，而过滤器是基于函数回调的。" class="headerlink" title="拦截器是基于java反射机制的，而过滤器是基于函数回调的。"></a>拦截器是基于java反射机制的，而过滤器是基于函数回调的。</h3><h3 id="过滤器依赖于servlet容器，而拦截器不依赖于servlet容器。"><a href="#过滤器依赖于servlet容器，而拦截器不依赖于servlet容器。" class="headerlink" title="过滤器依赖于servlet容器，而拦截器不依赖于servlet容器。"></a>过滤器依赖于servlet容器，而拦截器不依赖于servlet容器。</h3><h3 id="拦截器只对action起作用，而过滤器几乎可以对所有请求起作用。"><a href="#拦截器只对action起作用，而过滤器几乎可以对所有请求起作用。" class="headerlink" title="拦截器只对action起作用，而过滤器几乎可以对所有请求起作用。"></a>拦截器只对action起作用，而过滤器几乎可以对所有请求起作用。</h3><h3 id="拦截器可以访问action上下文、值栈里的对象，而过滤器不能。"><a href="#拦截器可以访问action上下文、值栈里的对象，而过滤器不能。" class="headerlink" title="拦截器可以访问action上下文、值栈里的对象，而过滤器不能。"></a>拦截器可以访问action上下文、值栈里的对象，而过滤器不能。</h3><h3 id="在action的生命周期里，拦截器可以多起调用，而过滤器只能在容器初始化时调用一次。"><a href="#在action的生命周期里，拦截器可以多起调用，而过滤器只能在容器初始化时调用一次。" class="headerlink" title="在action的生命周期里，拦截器可以多起调用，而过滤器只能在容器初始化时调用一次。"></a>在action的生命周期里，拦截器可以多起调用，而过滤器只能在容器初始化时调用一次。</h3><h2 id="关键代码"><a href="#关键代码" class="headerlink" title="关键代码"></a>关键代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.interceptor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.opensymphony.xwork2.ActionInvocation;</span><br><span class="line"><span class="keyword">import</span> com.opensymphony.xwork2.interceptor.AbstractInterceptor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">LoginInterceptor</span> <span class="keyword">extends</span> <span class="title class_">AbstractInterceptor</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">intercept</span><span class="params">(ActionInvocation actionInvocation)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="comment">//得到session key为users 的数据</span></span><br><span class="line">        <span class="type">Object</span> <span class="variable">users</span> <span class="operator">=</span> actionInvocation.getInvocationContext().getSession().get(<span class="string">&quot;users&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> actionInvocation.getInvocationContext().getName();</span><br><span class="line">        <span class="comment">//得到访问action 的名字 login.action 得到 login</span></span><br><span class="line">        System.out.println(name);</span><br><span class="line">        <span class="comment">//第一次登录时users 肯定为空,如果登录时找不到users 应该给它通过拦截器</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="literal">null</span> != users || <span class="string">&quot;login&quot;</span>.equals(name))</span><br><span class="line">            <span class="comment">//继续执行剩余的拦截器和action</span></span><br><span class="line">            <span class="keyword">return</span> actionInvocation.invoke();</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;login&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="struts-xml-拦截器配置"><a href="#struts-xml-拦截器配置" class="headerlink" title="struts.xml 拦截器配置"></a>struts.xml 拦截器配置</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">struts</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;default&quot;</span> <span class="attr">extends</span>=<span class="string">&quot;struts-default&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">interceptors</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--定义权限验证拦截器--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">interceptor</span> <span class="attr">name</span>=<span class="string">&quot;sessionUser&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.interceptor.LoginInterceptor&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">interceptor</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">interceptor-stack</span> <span class="attr">name</span>=<span class="string">&quot;LoginFlag&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">interceptor-ref</span> <span class="attr">name</span>=<span class="string">&quot;sessionUser&quot;</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">interceptor-ref</span> <span class="attr">name</span>=<span class="string">&quot;defaultStack&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">interceptor-stack</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">interceptors</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--定义默认拦截器--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">default-interceptor-ref</span> <span class="attr">name</span>=<span class="string">&quot;LoginFlag&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">package</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">struts</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>其中可以看到有一行配置 <code>&lt;interceptor-ref name=&quot;defaultStack&quot;/&gt;</code> defaultStack该拦截器用于实现数据的传输，如果不写的话会出现数据提取不到</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.jpg" alt="Jaron Chim 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.jpg" alt="Jaron Chim 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="post-tags">
              <a href="/tags/idea/" rel="tag"># idea</a>
              <a href="/tags/struts2/" rel="tag"># struts2</a>
              <a href="/tags/%E6%8B%A6%E6%88%AA%E5%99%A8/" rel="tag"># 拦截器</a>
              <a href="/tags/Interceptor/" rel="tag"># Interceptor</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2017/06/12/OGNL%E5%88%9B%E5%BB%BA%E5%92%8C%E9%85%8D%E7%BD%AE%E8%87%AA%E5%AE%9A%E4%B9%89%E6%97%A5%E6%9C%9F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E5%99%A8/" rel="prev" title="OGNL创建和配置自定义日期类型转换器">
                  <i class="fa fa-angle-left"></i> OGNL创建和配置自定义日期类型转换器
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2017/06/18/Hibernate%E4%B8%BB%E9%94%AE%E8%87%AA%E5%A2%9E%E9%95%BFincrement/" rel="next" title="Hibernate主键自增长increment">
                  Hibernate主键自增长increment <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">粤ICP备2025405034号-1 </a>
  </div>
  <div class="copyright">
    &copy; 2016 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-user"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Jaron Chim</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
